<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by Free CSS Templates
http://www.freecsstemplates.org
Released for free under a Creative Commons Attribution 2.5 License

Name       : Everyday Series
Description: A three-column, fixed-width blog design.
Version    : 1.0
Released   : 20090617

-->
<?php 
require_once('functions.php');
	session_start();
	if(isset($_SESSION['logout']) && $_SESSION['logout']==true){
		session_destroy();
	} else
	if (isset($_SESSION['username']) && !isset($_POST['logout'])) {
		header('Location: searchEmployee.php');}
		$userError = '';
		$passwordError ='';
		$errorMsg = '';
	
		if(isset($_POST['submit'])) {	
		
		$pass = $_POST['pass'];
		$user = $_POST['username'];
				
		if (isset($_POST['username']) && isset($_POST['pass']) && $_POST['username'] != '' && $_POST['pass'] != '') {

				$link = makeSQLconnection();
				
				$username = mysql_real_escape_string($_POST['username']);
				$userPass = mysql_real_escape_string($_POST['pass']);
				
				$q1 = sprintf("SELECT * FROM `USERS`");
				$r1 = mysql_query($q1, $link);
				
				$found = false;
				while($data = mysql_fetch_assoc($r1)){
					if($data['Username'] == $username) {
						$correctPassword=$data['Password'];
						$found = true;
					}
				}
		
				if (!$found) {
					$userError = "No such user.";
				}else{
					if ($correctPassword === $userPass) {
						$_SESSION['username'] = $username;
						$_SESSION['login']=true;
						header('Location: searchEmployee.php');
					} else {
						$passwordError ='Wrong password.';
							 
					}
				}
			
				mysql_close($link);
			} else {

					$errorMsg = 'Please fill in all the required information.';
			}
		}	
	
?>		
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Leave App Home</title>
<link href="template.css" rel="stylesheet" type="text/css" media="screen" />
<script src="jquery.js"></script>		
		<script>
			function registerpage(){
				window.location = "register.php";
			}		
				
			$(document).ready(function(){
								   
			   $("a").css({
				'text-decoration':'none',
			   });
			   $("a").hover(function () {
					$(this).css({'text-decoration':'none','color':'blue','font-weight':'bold'});
				  }, function () {
					var cssObj = {
					  'text-decoration':'none',
					  'color' : '',
					  'font-weight':''
					}
					$(this).css(cssObj);
				  });
							   			   
			   $("#submit").css({
					'background-color':'white',
					'color':'#2F2825'
				});
				$("#submit").hover(function () {
					$(this).css({'background-color' : '#993333','color':'white','font-weight':'bold'});
				  }, function () {
					var cssObj = {
					  'background-color' : 'white',
					  'color' : '#2F2825',
					  'font-weight':''
					}
					$(this).css(cssObj);
				  });				  
			  
			 });
		</script>
</head>
<body>
<!-- start header -->
<div id="header">
	<div id="logo">
		<h1><span>UPLB</span> &nbsp; Leave Application</h1>
		<p></p>
	</div>
</div>
<!-- end header -->
<div id="wrapper">
	<!-- start page -->
	<div id="page">
	<div id="page-bg">
		<div id="sidebar1" class="sidebar">			
		</div>
		<!-- start content -->
		<div id="content">
			<div class="post">
				<h1 class="title"><strong><a href="#">Welcome to our website!</a></strong></h1>
				<div class="entry">
					<br />
					<!--Login box. THIS SHOULD NOT APPEAR IF THE USER IS LOGGED IN-->
					<span class='error'><?php if ($errorMsg!='') {echo $errorMsg . "<br/>";}?></span>					
					<form method = "post" action = "login.php" align="center">
						<table>
						<tr>
							<td>
								Username:
							</td>
							<td>
								<input type ="text" name = "username" id = "username" value="<?php if(isset($_POST['submit'])) echo $_POST['username']; ?>"/>
							</td>
							<td>
								<?php 
									if(isset($_POST['submit'])){ 
										if($_POST['username'] == ""  ){ 
											echo "<span class='error'>Enter your correct username!</span>"; $c = 1; 
										} else if ($userError!='') {
											echo "<span class='error'>".$userError. "</span>";
										} else $c = 0;
									} 
								?>
							</td>
						</tr>						
						<tr>
							<td>
								Password: 
							</td>
							<td>
								<input type ="password" name = "pass" id = "pass" value="<?php if(isset($_POST['submit'])) echo $_POST['pass']; ?>"/>
							</td>
							<td>
								<?php 
									if(isset($_POST['submit'])){ 
										if($_POST['pass']== ""){ 
											echo "<span class='error'>Enter a password!</span>"; $d = 1; 
										} else if ($passwordError != '') {
											echo "<span class='error'>".$passwordError . "</span>";
										} else $d = 0;
									} 
								?>							
							</td>
						</tr>												
						<tr>
							<td>&nbsp;</td>
							<td>
								<input type = "submit" class = "buttons" name = "submit" id="submit" value = "Log In"/> 
							</td>
						</tr>
						</table>
					</form>
					<p class="links"><a href="#" onclick='registerpage()' class="more">&laquo;&laquo;&nbsp;&nbsp;Register&nbsp;&nbsp;&raquo;&raquo;</a></p>					
				</div>
			</div>
		</div>
		<!-- end content -->
		<!-- start sidebars -->
		<div id="sidebar2" class="sidebar">
		</div>
		<!-- end sidebars -->
		<div style="clear: both;">&nbsp;</div>
	</div>
	</div>
	<!-- end page -->
</div>
<div id="footer">	
</div>
</body>
</html>
